package finalgame.Graphics;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;

import javax.imageio.ImageIO;
import org.lwjgl.BufferUtils;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.GL11;

import finalgame.Util;

/**
 * 
 * @author Pat
 */
public class ScreenShot {
	private static DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd_HH.mm.ss");
	
	public static BufferedImage takeScreenShot() {
		GL11.glReadBuffer(GL11.GL_FRONT);
		int width = Display.getDisplayMode().getWidth();
		int height = Display.getDisplayMode().getHeight();
		int bpp = 4; // Assuming a 32-bit display with a byte each for red,
						// green, blue, and alpha.
		ByteBuffer buffer = BufferUtils.createByteBuffer(width * height * bpp);
		GL11.glReadPixels(0, 0, width, height, GL11.GL_RGBA,
				GL11.GL_UNSIGNED_BYTE, buffer);
		String format = "PNG"; // Example: "PNG" or "JPG"
		BufferedImage image = new BufferedImage(width, height,
				BufferedImage.TYPE_INT_RGB);

		for (int x = 0; x < width; x++) {
			for (int y = 0; y < height; y++) {
				int i = (x + (width * y)) * bpp;
				int r = buffer.get(i) & 0xFF;
				int g = buffer.get(i + 1) & 0xFF;
				int b = buffer.get(i + 2) & 0xFF;
				image.setRGB(x, height - (y + 1), (0xFF << 24) | (r << 16) | (g << 8) | b);
			}
		}
		return image;
	}

	public static File screenShot() {
		File dir = new File(Util.getWorkingDir(), "screenshots"); // The file to save to.
		dir.mkdir();
		File out = null;
		String s = dateFormat.format(new Date()).toString();
		
		for (int i = 1; (out = new File(dir, (s + (i != 1 ? "_" + i : "") + ".png"))).exists(); i++) ;
		
		String format = "PNG"; // Example: "PNG" or "JPG"

		try {
			ImageIO.write(takeScreenShot(), format, out);
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return out;
	}
}
